/**
 * Created by wenwang on 2017/8/28.
 */

/**
 * 日期格式化
 * @param format
 * @returns {*}
 */
export function format(date, format) {
  const o = {
    'M+': date.getMonth() + 1, //month
    'd+': date.getDate(), //day
    'h+': date.getHours(), //hour
    'm+': date.getMinutes(), //minute
    's+': date.getSeconds(), //second
    'q+': Math.floor((date.getMonth() + 3) / 3), //quarter
    'S': date.getMilliseconds() //millisecond
  }
  if (/(y+)/.test(format)) {
    format = format.replace(RegExp.$1, (`${date.getFullYear()}`).substr(4 - RegExp.$1.length))
  }
  for (const k in o) {
    if (new RegExp(`(${k})`).test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : (`00${o[k]}`).substr((`${o[k]}`).length))
    }
  }
  return format
}

/**
 * 判断年份是否为润年
 *
 * @param {Number} year
 */
export function isLeapYear(year) {
  return (year % 400 === 0) || (year % 4 === 0 && year % 100 !== 0)
}
/**
 * 获取某一年份的某一月份的天数
 *
 * @param {Number} year
 * @param {Number} month
 */
export function getMonthDays(year, month) {
  return [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month] || (isLeapYear(year) ? 29 : 28)
}
/**
 * 获取某年的某天是第几周
 * @param {Number} y
 * @param {Number} m
 * @param {Number} d
 * @returns {Number}
 */
export function getWeekNumber(y, m, d) {
  const now = new Date(y, m - 1, d)
  const year = now.getFullYear()
  const month = now.getMonth()
  let days = now.getDate()
  //那一天是那一年中的第多少天
  for (let i = 0; i < month; i++) {
    days += getMonthDays(year, i)
  }

  //那一年第一天是星期几
  const yearFirstDay = new Date(year, 0, 1).getDay() || 7

  let week = null
  if (yearFirstDay === 1) {
    week = Math.ceil(days / yearFirstDay)
  } else {
    days -= (7 - yearFirstDay + 1)
    week = Math.ceil(days / 7) + 1
  }

  return week
}

export function todayWeekNumber() {
  const now = new Date()
  const y = now.getFullYear()
  const m = now.getMonth() + 1
  const d = now.getDay()
  return getWeekNumber(y, m, d)
}

export function fmtcnstyle(datestr, all = true) {
  const ss = datestr.split(' ')
  const datess = ss[0].split('-')
  if (ss.length === 1 || !all) {
    if (datess.length === 2) {
      return `${datess[0]}年${datess[1]}月`
    }
    else if (datess.length === 3) {
      return `${datess[0]}年${datess[1]}月${datess[2]}日`
    }
    else {
      return datestr
    }
  }
  else {
    const timess = ss[1].split(':')
    return `${datess[0]}年${datess[1]}月${datess[2]}日 ${timess[0]}:${timess[1]}`
  }
}

export function translate(dataStr) {
  dataStr = dataStr.replace('年', '-')
  dataStr = dataStr.replace('月', '-')
  dataStr = dataStr.replace('日', ' ')
  return dataStr.trim()
}

export function dateDiff(startTime, endTime, diffType) {
  //将xxxx-xx-xx的时间格式，转换为 xxxx/xx/xx的格式
  startTime = startTime.replace(/\-/g, "/");
  endTime = endTime.replace(/\-/g, "/");
  //将计算间隔类性字符转换为小写
  diffType = diffType.toLowerCase();
  var sTime = new Date(startTime); //开始时间
  var eTime = new Date(endTime); //结束时间
  //作为除数的数字
  var timeType = 1;
  switch (diffType) {
    case"second":
      timeType = 1000;
      break;
    case"minute":
      timeType = 1000 * 60;
      break;
    case"hour":
      timeType = 1000 * 3600;
      break;
    case"day":
      timeType = 1000 * 3600 * 24;
      break;
    case "month":
      const smonth = sTime.getFullYear() * 12 + sTime.getMonth()
      const emonth = eTime.getFullYear() * 12 + eTime.getMonth()
      return emonth - smonth
      break;
    case "year":
      return eTime.getFullYear() - sTime.getFullYear()
    default:
      break;
  }
  return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(timeType));
}

export function months(date1, date2) {
  // 拆分年月日
  date1 = date1.split('-');
  date2 = date2.split('-');
  const stYear = parseInt(date1[0])
  const stMonth = parseInt(date1[1])
  const edYear = parseInt(date2[0])
  const edMonth = parseInt(date2[1])
  date1 = stYear * 12 + stMonth
  date2 = edYear * 12 + edMonth
  const m = Math.abs(date1 - date2)
  const ret = []
  let year = stYear, month = stMonth
  for (let i = 0; i < m+1; ++i) {
    let currmonth = month < 10 ? `0${month}` : `${month}`
    ret.push(`${year}-${currmonth}`)
    if (year === edYear && month === edMonth) {
      break;
    }
    ++month
    if (month > 12) {
      month = 1
      ++year
    }
  }
  return ret


}


